package com.yonyou.brigade.third_parties.smectrip.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.third_parties.smectrip.service.ICtripSmeOrderService;
import com.yonyou.brigade.third_parties.smectrip.service.ISmeCtripPublicService;
import com.yonyou.brigade.third_parties.smectrip.service.ISmeCtripUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by Administrator on 2019/1/2.
 */
@Controller
@RequestMapping("/ctripSme")
public class CtripSmeController {

    @Autowired
    @Qualifier("ctripSmeOrderServiceImpl")
    private ICtripSmeOrderService ctripSmeOrderService;

    @Autowired
    @Qualifier("briChannelServiceImpl")
    private IBriChannelService briChannelService;

    @Autowired
    @Qualifier("smeCtripPublicServiceImpl")
    private ISmeCtripPublicService smeCtripPublicService;

    @Autowired
    private ISmeCtripUserService smeCtripUserService;

    @RequestMapping("/syncUser2CtripSme")
    public void test(@RequestBody JSONObject param) {
        if (param != null && "135246".equals(param.getString("token"))) {
            smeCtripUserService.syncAllUsersToCtripSME(param.getString("tenant"), param.getString("config"), param.getString("phone"));
        }
    }

    @RequestMapping(value = "/getOrder",method = RequestMethod.POST)
    public @ResponseBody
    Map<String, String> testUcUser(@RequestBody JSONObject body){
        Map<String,String> result = new HashMap<>();
        String tenant = body.getString("tenant");
        String orgPk = body.getString("orgPk");
        String beginDate = body.getString("beginDate");
        String endDate = body.getString("endDate");
        String phone = body.getString("phone");

        ctripSmeOrderService.getCtripSmeOrderList(tenant,beginDate,endDate);

        result.put("result","成功");

        return result;
    }

    @RequestMapping(value = "/getOrderByOrderId",method = RequestMethod.POST)
    public @ResponseBody
    Map<String, String> getOrderByOrderId(@RequestBody JSONObject body){
        Map<String,String> result = new HashMap<>();
        String tenant = body.getString("tenant");
        String searchType = body.getString("searchType");
        String orderIds = body.getString("orderIds");

        List<String> lis = Arrays.asList(orderIds.split(","));

        ctripSmeOrderService.getCtripSmeOrderListByOrderId(searchType, lis, tenant);

        result.put("result","成功");

        return result;
    }

    /**
     * 将某一用户传给携程商旅通且设为管理员
     * @param body
     * @return
     */
    @RequestMapping(value = "/addAdminUser",method = RequestMethod.POST)
    public @ResponseBody
    Map<String, String> addAdminUser(@RequestBody JSONObject body){
        Map<String,String> result = new HashMap<>();
        String employeeID = body.getString("employeeID");
        String name = body.getString("name");
        String mobile = body.getString("mobile");
        String isUsed = body.getString("isUsed");
        String tenant = body.getString("tenant");
        BriChannel briChannel = briChannelService.getEnableChannelByPersonTenantOrgAndChannel(tenant,null,"ctripSME");
        if (BriChannel.SYNCUSER == briChannel.getSyncUser()) {
            JSONArray memberInfoModelArray = new JSONArray();
            JSONObject memberInfoModel = new JSONObject();
            memberInfoModel.put("EmployeeID",employeeID);
            memberInfoModel.put("Name",name);
            memberInfoModel.put("Mobile",mobile);
            memberInfoModel.put("IsUsed",isUsed);
            memberInfoModel.put("IsManager",true);
            memberInfoModelArray.add(memberInfoModel);
            smeCtripPublicService.syncCtripSmeUsers(tenant,memberInfoModelArray,briChannel.getConfig());
        }
        return result;
    }
}